﻿//-----------------------------------------------------------------------
// <copyright file="Category.xaml.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.Views
{
    using System.Diagnostics;
    using Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.MVVMSupport;
    using Windows.UI.Xaml.Controls;

    /// <summary>
    /// A page that displays an overview of a single group, including a preview of the items
    /// within the group.
    /// </summary>
    public sealed partial class Category : Page, IPageState
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Category"/> class.
        /// </summary>
        public Category()
        {
            this.InitializeComponent();
            this.projects.Focus(Windows.UI.Xaml.FocusState.Programmatic);
            if (!Windows.Storage.ApplicationData.Current.LocalSettings.Values.ContainsKey("Shown category appbar hint"))
            {
                hintPopup.IsOpen = true;
                Windows.Storage.ApplicationData.Current.LocalSettings.Values.Add("Shown category appbar hint", true);
            }
        }

        /// <summary>
        /// Gets the state of a page.
        /// </summary>
        /// <returns>The page state information</returns>
        public object GetState()
        {
            return projects.SelectedIndex;
        }

        /// <summary>
        /// Sets the state of a page.
        /// </summary>
        /// <param name="state">The state information.</param>
        public void SetState(object state)
        {
            var item = (int)state;
            if (item < 0)
            {
                return;
            }

            projects.UpdateLayout();
            projects.ScrollIntoView(projects.Items[item]);
        }

        /// <summary>
        /// Shows the filter options.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Windows.UI.Xaml.RoutedEventArgs"/> instance containing the event data.</param>
        private void ShowFilterOptions(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            filterOptions.IsOpen = true;
        }

        /// <summary>
        /// Closes the filter options.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Windows.UI.Xaml.RoutedEventArgs"/> instance containing the event data.</param>
        private void CloseFilterOptions(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            SetAppBarState(false);
            filterOptions.IsOpen = false;
        }

        /// <summary>
        /// Sets the state of the top &amp; bottom appbars.
        /// </summary>
        /// <param name="state">True is open, false is closed</param>
        private void SetAppBarState(bool state)
        {
            this.TopAppBar.IsOpen = state;
            this.BottomAppBar.IsOpen = state;
        }

        /// <summary>
        /// Raised when the right mouse button is clicked.
        /// </summary>
        /// <param name="sender">The source.</param>
        /// <param name="e">The parameters to the event.</param>
        private void Button_RightTapped(object sender, Windows.UI.Xaml.Input.RightTappedRoutedEventArgs e)
        {
            SetAppBarState(true);
            e.Handled = true;
        }

        /// <summary>
        /// Navigates to a project.
        /// </summary>
        /// <param name="sender">The source listview</param>
        private void NavigateToProject(object sender)
        {
            var listview = sender as ListView;
            var selectedProject = listview.SelectedItem as Models.Project;
            var viewModel = this.DataContext as ViewModels.Category;

            if (selectedProject == null || viewModel == null)
            {
                return;
            }

            viewModel.NavigateToSelectedProject(selectedProject);
        }

        /// <summary>
        /// Raised when the list view is tapped.
        /// </summary>
        /// <param name="sender">The source.</param>
        /// <param name="e">The parameters to the event.</param>
        private void ProjectsTapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            this.NavigateToProject(sender);
        }

        /// <summary>
        /// Raised when the keys are pressed
        /// </summary>
        /// <param name="sender">The source.</param>
        /// <param name="e">The parameters to the event.</param>
        private void ProjectsKeyUp(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
                this.NavigateToProject(sender);
                e.Handled = true;
            }
        }

        /// <summary>
        /// Is raised when the user hits the show Appbar button in the hint.
        /// </summary>
        /// <param name="sender">The source of the event</param>
        /// <param name="e">The event arguments.</param>
        private void ShowAppBar(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            hintPopup.IsOpen = false;
            SetAppBarState(true);
        }
    }
}
